package com.bocai.kelala.ui.travel.main.act;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bocai.kelala.App;
import com.bocai.kelala.R;
import com.bocai.kelala.base.BaseActivity;
import com.bocai.kelala.bean.MainListBean;
import com.bocai.kelala.bean.PhoneTypeBean;
import com.bocai.kelala.bean.UserBean;
import com.bocai.kelala.ui.travel.main.contract.MainContract;
import com.bocai.kelala.ui.travel.main.module.MainModule;
import com.bocai.kelala.ui.travel.main.presenter.MainPresenter;
import com.bocai.kelala.ui.travel.set.act.CertificationInformationActivity;
import com.bocai.kelala.ui.travel.set.act.CertificationInformationStateActivity;
import com.bocai.kelala.ui.travel.set.act.MyInvoiceAct;
import com.bocai.kelala.ui.travel.set.act.SetActivity;
import com.bocai.kelala.ui.travel.stroke.act.MyStrokeActivity;
import com.bocai.kelala.util.SP;
import com.bocai.kelala.util.ToastUitl;
import com.bumptech.glide.Glide;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by yuanfei on 2017/6/6.
 */

public class UserActivity extends BaseActivity implements View.OnClickListener,MainContract.view {
    @BindView(R.id.img_right)
    ImageView imgRight;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.head_img)
    CircleImageView headImg;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.ll_my_stroke)
    LinearLayout llMyStroke;
    @BindView(R.id.ll_my_certification)
    LinearLayout llMyCertification;
    @BindView(R.id.ll_my_invoice)
    LinearLayout llMyInvoice;
    @BindView(R.id.ll_set)
    LinearLayout llSet;
    @Inject
    MainPresenter presenter;

    @Override
    public int getLayoutId() {
        return R.layout.activity_user;
    }

    @Override
    public void initPresenter() {
        App.get(this).getAppComponent().addSub(new MainModule(this)).inject(this);
    }

    @Override
    public void initView() {
        llMyStroke.setOnClickListener(this);
        llMyInvoice.setOnClickListener(this);
        llSet.setOnClickListener(this);
        llMyCertification.setOnClickListener(this);
        imgRight.setOnClickListener(this);
        tvName.setText(SP.get(this,"PhoneNumber","").toString());
        Glide.with(this).load(SP.get(this,"Avatar2","").toString()).error(R.mipmap.head_img).into(headImg);
        if ((int)SP.get(mContext,"ApprovalStatus",0) == 1){
            presenter.getUser();
        }
    }

    @Override
    public void finish() {
        super.finish();
        overridePendingTransition(R.anim.without, R.anim.user_close);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.ll_my_invoice:
                if ((int) SP.get(this,"ApprovalStatus",0) == 0){
                    ToastUitl.showShort("认证未申请");
                } else if ((int)SP.get(this,"ApprovalStatus",0) == -1){
                    ToastUitl.showShort("审核未通过");
                }else if ((int)SP.get(this,"ApprovalStatus",0) == 1){
                    ToastUitl.showShort("审核中");
                }else {
                    startActivity(new Intent(this, MyInvoiceAct.class));
                }
                break;
            case R.id.ll_set:
                startActivityForResult(new Intent(this, SetActivity.class),1);
                break;
            case R.id.ll_my_certification:
                if ((int)(SP.get(this,"ApprovalStatus",0)) == 0) {
                    startActivity(new Intent(this, CertificationInformationActivity.class));
                }else {
                    startActivity(new Intent(this, CertificationInformationStateActivity.class));
                }
                break;
            case R.id.ll_my_stroke:
                if ((int)SP.get(this,"ApprovalStatus",0) == 0){
                    ToastUitl.showShort("认证未申请");
                } else if ((int)SP.get(this,"ApprovalStatus",0) == -1){
                    ToastUitl.showShort("审核未通过");
                }else if ((int)SP.get(this,"ApprovalStatus",0) == 1){
                    ToastUitl.showShort("审核中");
                }else {
                    startActivity(new Intent(this, MyStrokeActivity.class));
                }
                break;
            case R.id.img_right:
                finish();

        }
    }

    @Override
    public void getCode(String code) {

    }

    @Override
    public void getType(PhoneTypeBean typeBean) {

    }

    @Override
    public void getUser(UserBean userBean) {
        SP.putUser(UserActivity.this, userBean);
    }

    @Override
    public void getHomeListSuccess(MainListBean bean) {

    }

    @Override
    public void setBtnCode(int index) {

    }

    @Override
    public void showLoading(String title) {
        startProgressDialog(title);
    }

    @Override
    public void stopLoading() {
        stopProgressDialog();
    }

    @Override
    public void showErrorTip(String msg) {
        if (msg.equals("用户授权失败")){
            msg = "您的登录已失效，请重新登录";
            startActivity(new Intent(this,MainActivity.class).putExtra("index",1));
            finish();
        }
        showShortToast(msg);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1){
            tvName.setText(SP.get(this,"PhoneNumber","").toString());
            Glide.with(this).load(SP.get(this,"Avatar2","").toString()).error(R.mipmap.head_img).into(headImg);
        }
    }
}
